﻿using AutoMapper;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Tms;
using PeiYue.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Tms
{
    public class DriverBackRecordServices : BaseServices<DriverBackRecordDao>, IDriverBackRecordServices
    {
        IBaseRepository<DriverBackRecordDao> _dal;
        IMapper _mapper;
        public DriverBackRecordServices(IBaseRepository<DriverBackRecordDao> dal, IMapper mapper)
        {
            this._dal = dal;
            base.BaseDal = dal;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取司机的客户列表
        /// </summary>
        /// <param name="driverId">司机Id</param>
        /// <returns></returns>
        public async Task<List<CustomerResponseDto>> GetCustomerList(long driverId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select s.codec as CustomerCode,s.namec as CustomerName ");
            strSql.Append("from oms_wms_router_shop rs ");
            strSql.Append("left join oms_wms_router r on rs.router_id=r.id ");
            strSql.Append("left join oms_crm_customer_shop s on rs.shop_id=s.id ");
            strSql.Append("left join oms_wms_vehicle v on v.rt_id=r.id ");
            strSql.Append("left join oms_wms_driver d on d.vc_id=v.id ");
            strSql.Append("where rs.status=1 and rs.status=1 and r.status=1 and s.status=1 and v.status=1 and d.status=1  ");
            strSql.Append("and d.id=@driverId ");
            List<SugarParameter> parameters = new List<SugarParameter>();
            parameters.Add(new SugarParameter("@driverId", driverId));
            var list = await base.SqlQueryList<CustomerResponseDto>(strSql.ToString(), parameters);
            var customerList = list.Select(t => t.CustomerCode).ToList();
            if (customerList.Count == 0) return new List<CustomerResponseDto>();
            string codec = string.Join(",", customerList.Select(t => "'" + t + "'").ToArray());
            strSql.Clear();
            strSql.Append(@"select t.CustomerCode,t.CustomerName,t.SkuCode,t.SkuName,(t.Qty - isnull(t1.Qty, 0)) as Qty from(
   select h.客户编号 as CustomerCode, h.客户 as CustomerName, b.存货编码 as SkuCode, b.品名 as SkuName,
   abs(sum(b.数量)) as Qty
   from 供应链.dbo.v销售订单表头 (Nolock)h
   Left Join 供应链.dbo.v销售订单表体(Nolock)  b ON h.单号 = b.单号
   Left Join 供应链.dbo.v存货档案(Nolock)  c ON b.存货ID = c.fID
   where h.类型 = '退货订单' and h.客户编号 in (" + codec + ") and c.是否附随品 = '是' ");
            strSql.Append(@"
   group by h.客户编号, h.客户, b.存货编码, b.品名
   )t
   left join
   (
   select sum(Qty) Qty, CustomerCode, SkuCode  from oms.dbo.DriverBackRecord where CreateUser=@driverId group by CustomerCode, SkuCode
   )t1 on t.CustomerCode = t1.CustomerCode and t.SkuCode = t1.SkuCode ");
            var result = await base.SqlQueryList<CustomerResponseDto>(strSql.ToString(), parameters);
            return result;
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<MessageModel<string>> AddBackRecord(UpdateBackRecordRequestDto dto,long userId)
        {
            var message = new MessageModel<string>();
            DriverBackRecordDao dao = new DriverBackRecordDao();
            dao.CustomerCode = dto.CustomerCode;
            dao.SkuCode = dto.SkuCode;
            dao.Qty = dto.Qty;
            dao.CreateTime = DateTime.Now;
            dao.CreateUser = userId;
            dao.UpdateTime = DateTime.Now;
            dao.UpdateUser = userId;
            await _dal.Add(dao);
            message.status = 200;
            message.msg = "成功";
            return message;
        }
    }
}
